package com.bw.module_main.dialog

import android.graphics.Color
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.util.DisplayMetrics
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.bw.module_main.R


/**
 * @author ytx
 * @date 2025-09-18 14:33
 * @description
 */
class MessageNoticeDialog(
    private val title: String? = null,  //标题
    private val message: SpannableStringBuilder? = null, //内容
    private val onConfirmClick: (() -> Unit)? = null, //确认回调
    private val onCancelClick: (() -> Unit)? = null,//取消回调
    private val onlyConfirmButton: Boolean = false, //是否只有确定按钮
    private val confirmText: String = "确定",
    private val cancelText: String = "取消",
    private val cancelable: Boolean = false, //点击外部是否可以取消
    private val alignment: Int = Gravity.CENTER, //对齐方式
    private val confirmClickDismiss: Boolean = true //点击确认按钮后是否关闭
):DialogFragment() {
    companion object{
        const val TAG = "MessageNoticeDialog"
    }
    private lateinit var mParentView:View



//
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //设置样式
        setStyle(STYLE_NO_TITLE, com.bw.lib_common.R.style.MessageNoticeDialog)
    }

    override fun onStart() {
        super.onStart()
        val window = dialog!!.window //对话框窗体
        val wlp = window!!.attributes //属性
        //屏幕
        val dm = DisplayMetrics()
        activity?.windowManager?.defaultDisplay?.getMetrics(dm)
        ///设置属性
        window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
        wlp.gravity = Gravity.CENTER
        wlp.width = (dm.widthPixels * 0.72).toInt()
        window.attributes = wlp
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mParentView = inflater.inflate(R.layout.dialog_message_notice,container,false)
        return mParentView
    }

    /**
     *  private val title: String? = null,  //标题
     *     private val message: String? = null, //内容
     *     private val onConfirmClick: (() -> Unit)? = null, //确认回调
     *     private val onCancelClick: (() -> Unit)? = null,//取消回调
     *     private val onlyConfirmButton: Boolean = false, //是否只有确定按钮
     *     private val confirmText: String = "确定",
     *     private val cancelText: String = "取消",
     *     private val cancelable: Boolean = false, //点击外部是否可以取消
     *     private val alignment: Int = Gravity.CENTER, //对齐方式
     *     private val confirmClickDismiss: Boolean = true //点击确认按钮后是否关闭
     */
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        dialog?.setCancelable(cancelable)
        dialog?.setCanceledOnTouchOutside(cancelable)

        mParentView.findViewById<TextView>(R.id.tv_message).apply {
            text = message
            gravity = alignment
            //富文本
            highlightColor = Color.TRANSPARENT //去掉蓝色下划线
            movementMethod = LinkMovementMethod.getInstance() //点击有效果
        }
        mParentView.findViewById<TextView>(R.id.btn_cancel).apply {
            text = cancelText
            if (onlyConfirmButton){
                visibility = View.GONE
            }
            setOnClickListener {
                onCancelClick?.invoke() //取消回调
            }
        }
        mParentView.findViewById<TextView>(R.id.btn_confirm).apply {
            text = confirmText
            setOnClickListener {
                onConfirmClick?.invoke()//回调
                if (confirmClickDismiss){//点击确认关闭对话框
                    dismiss()
                }
            }
        }
        mParentView.findViewById<TextView>(R.id.tv_title).text = title
    }

    /**
     * 显示对话框
     */
    fun show(fragmentManager:FragmentManager){
        if (fragmentManager.findFragmentByTag(TAG)?.isVisible == true){ //如果已经显示，直接返回
            return
        }
        this.show(fragmentManager, TAG)
    }
}